package com.fili.utilities.fontExporter;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Nov 9, 2006
 * Time: 1:11:54 PM
 * Revision: 0
 */
public class FontPanel extends JComponent implements Scrollable
{
    private int texSize;
    private int texDiv;
    private Color transparentBlack = new Color(0x00000000);
    private Color opaqueWhite = new Color(0xffffffff);

    public int getTexSize()
    {
        return texSize;
    }

    public void setTexSize(int texSize)
    {
        this.texSize = 1;
        while (this.texSize < texSize)
            this.texSize *= 2;

        texDiv = texSize / 16;

        revalidate();
        repaint();
    }

    public FontPanel()
    {
        setTexSize(512);
    }


    protected void paintComponent(Graphics g)
    {
        g.setFont(g.getFont().deriveFont(Font.PLAIN, 20));
        g.setColor(transparentBlack);
        g.fillRect(0, 0, getWidth(), getHeight());
        for (int i = 0; i < 16; i++)
            for (int j = 0; j < 16; j++)
            {
                int x, y, w, h,h1;
                char c = (char) (i * 16 + j);

                x = j * texDiv;
                y = i * texDiv;
                w = g.getFontMetrics().stringWidth("" + c);
                h = g.getFontMetrics().getHeight()+g.getFontMetrics().getDescent();
                h1 = g.getFontMetrics().getHeight();

                if ((i + j) % 2 == 0)
                    g.setColor(Color.yellow);
                else
                    g.setColor(Color.green);
                g.fillRect(j * texDiv, i * texDiv, texDiv, texDiv);
                g.setColor(Color.red);
                g.fillRect(x, y, w, h);
                g.setColor(transparentBlack);
                g.drawString("" + c, x, y+h1);
            }
    }


    public Dimension getPreferredSize()
    {
        return new Dimension(texSize, texSize);
    }

    public Dimension getPreferredScrollableViewportSize()
    {
        return new Dimension(texSize, texSize);
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
    {
        return texSize / 16;
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
    {
        return texSize / 4;
    }

    public boolean getScrollableTracksViewportWidth()
    {
        return false;
    }

    public boolean getScrollableTracksViewportHeight()
    {
        return false;
    }
}
